<template>
	<view class="business">
		<view class="top">
			<view class="address">
				<uni-icons type="location"></uni-icons>
				<text>{{current_area}}</text>
			</view>
			<view class="inputbox">
				<uni-icons type="search"></uni-icons>
				<input type="text" placeholder="搜索商家" @click="toPage('./search/search')">
			</view>
		</view>
		<view class="navList">
			<view class="navitem" @click="toPage('list/offlist/list?offid='+ item.id)"
				v-for="(item,index) in class_list" :key="item.id">
				<image :src="item.img_url | getImg" mode=""></image>
				<text>{{item.name}}</text>
			</view>


		</view>
		<view class="businessbox">
			<!-- <view class="address">
				当前位置：{{address}}
			</view> -->
			<view class="businesslist">
				<view class="b_item" @click="toPage('detail/premium?offid=' + item.id)"
					v-for="(item,index) in shop_list" :key="item.id">
					<image :src="item.img | getImg" mode=""></image>
					<view class="infobox">
						<view class="name">
							<text>{{item.shop_name}}</text>
							<text class="color_yellow">{{item.main_project}}</text>
						</view>
						<text class="add_m">进店逛逛</text>
						<!-- <view class="address_name">
							{{item.shop_address}}
						</view> -->
					</view>
				</view>

			</view>
		</view>
	</view>
</template>

<script>
	import {
		OfflineClass,
		OfflineShop
	} from "@/api/api.js"
	import {
		getShopArea
	} from "@/api/user.js"
	export default {
		data() {
			return {
				class_list: [],
				shop_list: [],
				latitude: '',
				longitude: '',
				address: '',
				page: 1,
				toggle: true,
				current_area: '请选择',
				search_latitude: '',
				search_longitude: '',
			}
		},
		onLoad() {
			this.OfflineClass()
			this.ShopData()
			this.getShopArea()
			console.log(new Date().getTime());
		},
		onShow() {

		},
		onReachBottom() {
			if (this.toggle) {
				this.page++
				this.ShopData()
			}
		},
		methods: {
			choose_addr() {
				let that = this
				uni.getLocation({
					type: 'gcj02',
					altitude: false,
					geocode: true,
					isHighAccuracy: false,
					highAccuracyExpireTime: 100,
					success: function(res) {
						console.log(res);
						uni.chooseLocation({
							latitude: res.latitude,
							longitude: res.longitude,
							success: (data) => {
								console.log(data);
								that.current_area = data.name
								that.search_latitude = data.latitude
								that.search_longitude = data.longitude
								//指定位置搜索附近商家
								that.page = 1
								that.shop_list = []
								that.ShopData()
							}
						})
					}
				});
			},
			getShopArea() {

				uni.getLocation({
					altitude: false,
					geocode: true,
					isHighAccuracy: false,
					highAccuracyExpireTime: 100,
					success: (data) => {
						uni.getStorage({
							key: 'nowAddress',
							success: (res) => {
								console.log(111, res);
								if (res.data.expire) {
									let now_time = new Date().getTime()
									if (now_time >= res.data.expire) {
										uni.removeStorageSync('nowAddress')
										this.getShopAreas(data)
									} else {
										let a = Number(data.latitude) - Number(res.data.latitude)
										if (a < 0.0001 && a > -0.0001) {
											this.address = res.data.address
											this.current_area = res.data.address_title
											return
										} else {
											this.getShopAreas(data)
										}
									}

								} else {
									//没有缓存参数 重新设置
									uni.removeStorageSync('nowAddress')
									this.getShopAreas(data)
								}

							},
							fail: (err) => {
								this.getShopAreas(data)
							}
						})
					}
				})

			},
			getShopAreas(data) {
				getShopArea({
					latitude: data.latitude,
					longitude: data.longitude
				}).then(res => {
					console.log(res);
					this.address = res.data.address
					if (res.data.address_reference.landmark_l1) {
						this.current_area = res.data.address_reference.landmark_l1.title
					} else {
						this.current_area = res.data.address_reference.landmark_l2.title
					}

					uni.setStorage({
						key: 'nowAddress',
						data: {
							address: this.address,
							address_title: this.current_area,
							latitude: data.latitude,
							longitude: data.longitude,
							ad_info: res.data.ad_info,
							expire: new Date().getTime() + 24 * 60 * 60 * 1000 //一天有效期
						}
					})
				})
			},
			OfflineClass() {
				OfflineClass().then(res => {
					this.class_list = res.data
				})
			},
			ShopData() {
				OfflineShop({
					page: this.page,
					search_latitude: this.search_latitude,
					search_longitude: this.search_longitude
				}).then(res => {
					if (res.data.length > 0) {
						this.shop_list = this.shop_list.concat(res.data)
					} else {
						this.toggle = false
					}
				})
			},
			toPage(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #f3f3f3;
	}

	.top {
		display: flex;
		padding: 10px;
		justify-content: space-between;
		box-sizing: border-box;
		align-content: center;

		.address {
			font-size: 14px;
			height: 30px;
			line-height: 30px;
			;

			text {
				margin-right: 8px;
			}
		}

		.inputbox {
			display: flex;
			position: relative;
			width: 35%;

			text {
				position: absolute;
				left: 12px;
				top: 50%;
				transform: translate(0, -50%);
			}

			input {
				padding-left: 30px;
				box-sizing: border-box;
				background-color: #ddd;
				font-size: 14px;
				border-radius: 20px;
				height: 30px;
			}
		}
	}

	.businessbox {
		background: #fff;
		margin-top: 10px;
		padding: 10px;

		.address {
			font-size: 14px;
			padding-bottom: 10px;
			border-bottom: 5px solid #f3f3f3;
		}

		.b_item {
			display: flex;
			border-bottom: 1px solid #ccc;
			padding: 10px 0;
			align-items: center;

			image {
				height: 50px;
				width: 50px;
			}

			.infobox {
				margin-left: 10px;
				display: flex;
				justify-content: space-between;
				align-items: center;
				flex: 1;

				// .gap,
				// .address_name {
				// 	font-size: 14px;
				// 	color: #777;
				// 	margin-top: 5px;
				// }

				.name {
					display: flex;
					flex-direction: column;
					width: calc(100% - 80px);
					padding-right: 5px;
					box-sizing: border-box;

					.color_yellow {
						color: #ffb400;
						font-size: 12px;
						margin-top: 8px;
					}
				}

				.add_m {
					font-size: 14px;
					color: #000;
					border: 1px solid #ccc;
					height: 35px;
					border-radius: 5px;
					padding: 5px 10px;
					width: 80px;
					box-sizing: border-box;
					line-height: 23px;
				}
			}
		}
	}

	.navList {
		display: table;
		background-color: #fff;
		width: 100%;

		.navitem {
			float: left;
			width: 20%;
			text-align: center;
			padding: 10px 5px;
			box-sizing: border-box;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 14px;

			image {
				width: 50px;
				height: 50px;
				// border-radius: 50%;	
			}
		}

		.active {
			color: #FF5555;
			border-bottom: 2px solid #FF5555;
		}
	}
</style>
